define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'member/order/index' + location.search,
                    add_url: 'member/order/add',
                    edit_url: 'member/order/edit',
                    del_url: 'member/order/del',
                    multi_url: 'member/order/multi',
                    import_url: 'member/order/import',
                    table: 'member_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'linkman', title: __('Linkman'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'linkphone', title: __('Linkphone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'delivery_fee', title: __('Delivery_fee'), operate:'BETWEEN'},
                        {field: 'method', title: __('Method'), searchList: {"delivery":__('Delivery'),"pickup":__('Pickup')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_status', title: __('Pay_status')},
                        {field: 'cancel_time', title: __('Cancel_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'riderAssign',
                                    text: '详情',
                                    title: '详情',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-flag',

                                    click: function (e, row) {
                                        console.log('riderAssign', e, row);
                                        Controller.api.showRiderDialog(row.id);
                                    }
                                },
                                {
                                    name: 'send',
                                    text: '发货',
                                    title: '发货',
                                    icon: 'fa fa-clock-o',
                                    extend: 'data-area=\'["80%","80%"]\'',
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    url: 'member/order/send?id={id}',
                                    visible:function(row){
                                        if(row.status ==1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                },

                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        send: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

            showRiderDialog: function (id) {
                Fast.api.open('member/items/index?order_id='+id, '商品列表', {

                });
            },
        }
    };
    return Controller;
});
